package com.example.minio.component;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.errors.MinioException;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by YangGang on 2021/11/8 16:31。
 */


public class FileUploader {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException {
        try {
            // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(
                    "http://10.148.244.210:9000",
                    "minioadmin",
                    "minioadmin");

            // 获得对象的元数据。
            ObjectStat objectStat = minioClient.statObject("test", "12-1Z41G35547.jpg");
            System.out.println(objectStat);

            String url = minioClient.presignedGetObject("test", "12-1Z41G35547.jpg", 60 * 60 * 24 * 7);
            System.out.println(url);

        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }


    public static void main1(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException {
        try {
            // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(
                    "http://10.148.244.210:9000",
                    "minioadmin",
                    "minioadmin");

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists("test");
            if (isExist) {
                System.out.println("Bucket already exists.");
            } else {
                // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
                minioClient.makeBucket("test");
            }

            // 使用putObject上传一个文件到存储桶中。
            minioClient.putObject("test", "12-1Z41G35547.jpg", "C:\\Users\\Administrator\\Pictures\\12-1Z41G35547.jpg", null);
            System.out.println("minioClient.putObject");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }




}